package com.zyiot.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.umeng.socialize.bean.SocializeEntity;
import com.umeng.socialize.controller.RequestType;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners.SocializeClientListener;
import com.zyiot.cache.ImageDownloader;
import com.zyiot.cache.OnImageDownload;
import com.zyiot.lbs.R;
import com.zyiot.model.Eshop;

public class EshopAdapter extends BaseAdapter {
	private LayoutInflater mInflater;
	private List<Eshop> items;
	public static final int MSG_NET_TIMEOUT = 100;
	public static final int MSG_NET_STATUS_ERROR = 200;
	public static final int MSG_NET_SUCC = 1;
	private ViewHolder holder;
	private boolean mBusy = false;
	private Context context;
	Activity mActivity;
	ListView mListView;
	
	// private ImageLoader mImageLoader;
	ImageDownloader mDownloader;

	public void setFlagBusy(boolean busy) {
		this.mBusy = busy;
	}

	

	public EshopAdapter(Context context, List<Eshop> list, Activity mActivity, ListView mListView) {
		mInflater = LayoutInflater.from(context);
		items = list;
		this.context = context;
		this.mActivity = mActivity;
		this.mListView = mListView;
		// mImageLoader = new ImageLoader(context);
	}

	// public ImageLoader getImageLoader() {
	// return mImageLoader;
	// }

	@Override
	public int getCount() {
		return items.size();
	}

	@Override
	public Object getItem(int position) {
		return items.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	/* class ViewHolder */
	private class ViewHolder {
		TextView addr;
		TextView name;
		TextView distance;
		TextView comments;
		TextView likes;
		ImageView icon;
	}
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.content_eshop, null);
			holder = new ViewHolder();

			holder.name = (TextView) convertView.findViewById(R.id.name);
			holder.addr = (TextView) convertView.findViewById(R.id.addr);
			holder.distance = (TextView) convertView.findViewById(R.id.distance);
			holder.icon = (ImageView) convertView.findViewById(R.id.icon_eshop_logo);
			holder.comments = (TextView) convertView.findViewById(R.id.comments);
			holder.likes = (TextView) convertView.findViewById(R.id.likes);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
//		final TextViewPair textviewpair = new TextViewPair(holder.likes, null, holder.comments);
//		final Eshop eshop = items.get(position);
//		final UMSocialService controller = initAcitonBar(context, textviewpair, eshop);
		
		holder.addr.setText(items.get(position).getAddr() + "");
		holder.name.setText(items.get(position).getName() + "");
		holder.distance.setText(items.get(position).getDistance() + "");
		holder.comments.setText(items.get(position).getComments() + "");
		holder.likes.setText(items.get(position).getLikes() + "");

		if (mDownloader == null) {
			mDownloader = new ImageDownloader();
		}

		String url = "";
		url = items.get(position).getImageurl();
		holder.icon.setTag(url);
		// 这句代码的作用是为了解决convertView被重用的时候，图片预设的问题
		holder.icon.setImageResource(R.drawable.default_business);
		String imagePath = "/zyiot";

		if (mDownloader != null && !url.equals("")) {
			mDownloader.imageDownload(url, holder.icon, imagePath, mActivity, new OnImageDownload() {
				@Override
				public void onDownloadSucc(Bitmap bitmap, String c_url) {
					if (!c_url.equals("")) {
						ImageView imageView = (ImageView) mListView.findViewWithTag(c_url);
						if (imageView != null && c_url.equals(imageView.getTag())) {
							imageView.setImageBitmap(bitmap);
							imageView.setTag("");
						} else if (imageView != null) {
							imageView.setImageResource(R.drawable.default_business);
						}
					}
				}
			});
		}
		return convertView;
	}
	static String DESCRIPTOR;
	protected static UMSocialService initAcitonBar(Context context, TextViewPair textviewPair,Eshop eshop) {
		final TextViewPair mPair = textviewPair;
		DESCRIPTOR = eshop.getGeotable_id() + ":" + eshop.getUid() + ":" + eshop.getName();
		UMSocialService controller = UMServiceFactory.getUMSocialService(DESCRIPTOR, RequestType.SOCIAL);
		if (!controller.getEntity().mInitialized) {
			controller.setShareContent(eshop.getName()+"/t"+eshop.getAddr()+" 快来体验下吧！");
			controller.initEntity(context, new SocializeClientListener() {
				@Override
				public void onStart() {
				}
				@Override
				public void onComplete(int status, SocializeEntity entity) {
					matchEntity(mPair, entity);
				}

			});
		} else {
			matchEntity(mPair, controller.getEntity());
		}
		return controller;
	}

	private synchronized static void matchEntity(final TextViewPair mPair, SocializeEntity entity) {
		if (entity != null) {
			mPair.likeBt.setText("	：  " + entity.getLikeCount());
			mPair.commentBt.setText("	:  " + entity.getCommentCount());
			//mPair.shareBt.setText(":  " + entity.getShareCount());
		}
	}

	private static class TextViewPair {
		TextView likeBt, shareBt, commentBt;
		public TextViewPair(TextView likeBt, TextView shareBt, TextView commentBt) {
			super();
			this.likeBt = likeBt;
			//this.shareBt = shareBt;
			this.commentBt = commentBt;
		}

	}

}
